<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();
if (!isset($_SESSION['Username']) || !isset($_SESSION['Password'])) {
    header('Location: index.php');
}
require("database/profile_handler.php");
require 'database/message_handler.php';

if (isset($_POST["SubmitMessage"])) {
    if ($_POST['Subject'] == NULL || $_POST['BodyMessage'] == NULL){
        echo "<font color='red'>Message must have a subject and not empty. </font><br/>";
    }
	
        //$ArrayUser = $_SESSION["Username"];
    else{
        $ArrayUser = explode(' ', $_POST["UserName"]); //dapat array user
        $jum = count($ArrayUser);
        $ArrayUser[$jum] = $_SESSION["Username"];
        $MH = new message_handler();
        $MH->compose_message($ArrayUser, $_POST["Subject"], $_POST["BodyMessage"]);

        header("Location: homepage.php?msgsucc=1");
    }
}

if (isset($_GET['UserTujuan'])){
    $ut = $_GET['UserTujuan'];
}
else{
    $ut = NULL;
}

$pr = new profile_handler();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>Message Threads</title>
        <script type="text/javascript" src="script/validator.js"></script>
        <script type="text/javascript" src="script/calendar.js"></script>
        <script type="text/javascript" src="script/ajax.js"></script>
        <script type="text/javascript">
                                viewMessageList(99);
                            </script>

        <link rel="stylesheet" href="style/style.css" />
    </head>
    <body onload="loadPageHide()" >
        <?php
        if ($_GET['gagal'] == 1) {
        ?>
            <script type="text/javascript">alert("Operation failed");</script>
        <?
        }
        
        ?>
                    <div id="title_new_thread"><strong>Create new Thread</strong><br/><br/></div>
                    <div id="new_thread">
                    <form class="Message" id = "Message" name ="Message" action="message.php" method="POST" onsubmit=''>
                        <p>
                            <label id="LabelUserName"> To</label> <span id="UserNameSpan"> : </span> <input type="text" id="thread_to" name="UserName" value="<?php echo $ut;?>"/> <label class="Peringatan" id="LabelUserNameSalah"> </label>
                            <br />
                            <label id="LabelSubject"> Subject</label> <span id="SubjectSpan"> : </span> <input type="text" id="thread_sub" name="Subject"/> <label class="Peringatan" id="LabelSubjectSalah"> </label>
                            <br />
                            <label id="LabelBody"> Body</label> <span id="BodySpan"> : </span> <textarea id="thread_body" name="BodyMessage" cols="50" rows="7"></textarea> </label>
                            <br />
                            <input type="submit" id="thread_submit" name ="SubmitMessage" value="Create" />
                        </p>
                    </form>
                    </div>
                    <p>
                    <div id="title_list_thread"><strong>My Threads : </strong></div><br/>
                    <div id="message_container">
                        <div id="message_content_0">

                        </div>
                    </div>
                    </p>
    </body>
</html>
